#include "message.h"

Message::Message(uint32_t id, const std::string &name)
    : m_id(id), m_name(name), m_data(MemoryObjectPool::instance().take())
{

}

Message::Message(const Message &other)
{
    m_id = other.m_id;
    m_name = other.m_name;
    m_data = MemoryObjectPool::instance().take();
}

Message::Message(Message &&other)
{
    m_id = other.m_id;
    m_name.swap(other.m_name);
    m_data.swap(other.m_data);
}

Message::~Message()
{
    MemoryObjectPool::instance().give(std::move(m_data));
    m_data.reset();
}

Message &Message::operator=(const Message &other)
{
    if(&other != this) {
        m_id = other.m_id;
        m_name = other.m_name;
        m_data = MemoryObjectPool::instance().take();
    }
    return *this;
}

Message &Message::operator=(Message &&other)
{
    m_id = other.m_id;
    m_name.swap(other.m_name);
    m_data.swap(other.m_data);
    return *this;
}



uint32_t Message::id() const
{
    return m_id;
}

const std::string &Message::name() const
{
    return m_name;
}

// const std::vector<uint8_t> &Message::data() const
// {
//     return m_data->buffer();
// }

// void Message::setData(const std::vector<uint8_t> &data)
// {
//     m_data->buffer() = data;
// }

// void Message::setData(std::vector<uint8_t> &&data)
// {
//     m_data->buffer().swap(data);
// }

uint8_t *Message::data()
{
    return m_data->data();
}

size_t Message::size() const
{
    return m_data->totalSize();
}

bool Message::fill(uint8_t *data, size_t size, size_t offset)
{
    if(offset + size > this->size())
        return false;
    memcpy(this->data()+offset, data, size);
    return true;
}
